package io.github.heollhai.common.utils;

import io.github.heollhai.common.config.exceptions.GraceException;
import io.github.heollhai.common.entity.vo.date.StartAndEndVo;
import org.apache.commons.lang3.StringUtils;

import javax.swing.text.DateFormatter;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class DateUtil {
    /**
     * 年月格式
     */
    public static final DateTimeFormatter YEAR_MONTH_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM");
    /**
     * 日期格式
     */
    public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    /**
     * 日期时间格式
     */
    public static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /**
     * 日期时间格式
     */
    public static final DateTimeFormatter DATE_TIME_FORMAT_2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 获取时间戳
     */
    public static long getTimestamp() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前时间
     */
    public static LocalDateTime getCurrentLocalDateTime() {
        return LocalDateTime.now();
    }

    /**
     * 获取当前时间字符串格式
     */
    public static String getCurrentLocalDateStr() {
        return getCurrentLocalDateTime().format(DATE_TIME_FORMAT);
    }
    /**
     * 获取当前时间字符串格式
     */
    public static String getCurrentLocalDateStr(DateTimeFormatter formatter) {
        return getCurrentLocalDateTime().format(formatter);
    }

    /**
     * 根据字符串时间获取当月的开始结束时间
     */
    public static StartAndEndVo getMonthStartAndEnd(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            GraceException.display("时间不能为空");
        }
        YearMonth yearMonth = YearMonth.parse(dateStr, YEAR_MONTH_FORMAT);
        return StartAndEndVo.builder().start(yearMonth.atDay(1)).end(yearMonth.atEndOfMonth()).build();
    }

    /**
     * 两个日期之间的秒数差
     */
    public static long getSecondsBetween(String startTime, String endTime) {
        LocalDateTime dateTime1 = LocalDateTime.parse(startTime, DATE_TIME_FORMAT);
        LocalDateTime dateTime2 = LocalDateTime.parse(endTime, DATE_TIME_FORMAT);
        return  Duration.between(dateTime1, dateTime2).getSeconds();

    }





    // =======================================判断相关===========start==============================================

    /**
     * 判断时间戳是否在 N 秒内过期
     *
     * @param timestamp 时间戳
     * @param N         秒
     */
    public static boolean isTimestampExpired(long timestamp, long N) {
        return (getTimestamp() - timestamp) > N * 1000;
    }

    /**
     * 判断是否在时间区间内
     */
    public static boolean isInRange(LocalDateTime startTime, LocalDateTime endTime) {
        LocalDateTime now = LocalDateTime.now();
        return !now.isBefore(startTime) && !now.isAfter(endTime);
    }

    // =======================================判断相关===========end==============================================


}
